package com.example.huadu_server.appointment.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.huadu_server.appointment.entity.Appointment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;


@Mapper
public interface AppointmentMapper extends BaseMapper<Appointment> {
    @Select("SELECT house_id as houseId, COUNT(*) as count FROM appointment WHERE user_id = #{userId} GROUP BY house_id")
    List<Map<String, Object>> getHouseAppointmentCountsByUser(@Param("userId") int userId);

    /**
     * 查询预约趋势统计
     * @param startDate
     * @param endDate
     * @param granularity
     * @return
     */
    @Select("<script>" +
            "SELECT DATE_FORMAT(appointment_date, " +
            "<choose>" +
            "  <when test='granularity == \"day\"'> '%Y-%m-%d' </when>" +
            "  <when test='granularity == \"month\"'> '%Y-%m' </when>" +
            "  <when test='granularity == \"year\"'> '%Y' </when>" +
            "  <otherwise> '%Y-%m-%d' </otherwise>" +
            "</choose>) AS period, " +
            "COUNT(*) AS count " +
            "FROM appointment " +
            "WHERE appointment_date BETWEEN #{startDate} AND #{endDate} " +
            "GROUP BY period ORDER BY period" +
            "</script>")
    List<Map<String, Object>> selectTrendStats(@Param("startDate") String startDate,
                                               @Param("endDate") String endDate,
                                               @Param("granularity") String granularity);

    /**
     * 根据预约记录ID查询预约记录
     * @param scheduleId
     * @return
     */
    @Select("SELECT * FROM appointment WHERE schedule_id = #{scheduleId}")
    List<Appointment> selectByScheduleId(@Param("scheduleId") Integer scheduleId);
}
